package ModelImplementation

import (
	"SPC/Model"
	"time"
)

type CommissionedClassification struct {
	itsCommissionRate float64
	itsSalary         float64
	itsReceipts       map[time.Time]SalesReceipt
}

func NewCommissionedClassification(salary float64, CommissionRate float64) *CommissionedClassification {
	return &CommissionedClassification{
		itsSalary: salary,
		itsCommissionRate: CommissionRate,
	}
}

func (c *CommissionedClassification)GetSalary() float64 {
	return c.itsSalary
}

func (c *CommissionedClassification)AddSalesReceipt(sr SalesReceipt){
	c.itsReceipts[sr.GetSaleDate()] = sr
}

func(c *CommissionedClassification)GetReceipt(date time.Time) SalesReceipt {
	return c.itsReceipts[date]
}

func (c *CommissionedClassification)GetRate() float64 {
	return c.itsCommissionRate
}

func (c *CommissionedClassification)CalculatePay(pc Model.Paycheck)float64{
	commission := 0.0
	for k, _ := range c.itsReceipts{
		receipt := c.itsReceipts[k]
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()
		
		if (k.After(dt1) && k.Before(dt2)) {
			commission += receipt.GetAmount() * c.itsCommissionRate
		}
	}
	return c.itsSalary + commission
}
